/*-----------------------------------------------------------------------------
|                               File description                              |
-----------------------------------------------------------------------------*/
/**
 * @file      : main.c
 * @author    : liuzhihua (liuzhihuawy@163.com)
 * @brief     : TODO:
 * @version   : v0.0.1
 * @date      : 2024-11-22
 * @editor    : Encoding UTF-8 | 1 tab = 4 spaces | 1 line <= 80 characters
 * @license   : TODO:See www.runoob.com/w3cnote/open-source-license.html
 * @Copyright (c) 2024 by liuzhihua. All rights reserved.
 */
/*-----------------------------------------------------------------------------
|                                 Includes begin                              |
-----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdarg.h>
#include <malloc.h>
#include "template.h"
/*-----------------------------------------------------------------------------
|                                 Includes end                                |
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
|                                 Declare begin                               |
-----------------------------------------------------------------------------*/
template_t htemplate1;
static int g_malloc_cnt = 0;
static int g_free_cnt = 0;
/*-----------------------------------------------------------------------------
|                                 Declare end                                 |
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
|                          Funciton definition begin                          |
-----------------------------------------------------------------------------*/

int ex_template_log(int level, const char *format, ...)
{
    char log_buf[256];
    va_list args;
    va_start(args, format);
    printf("[LV.%d]", level);
    size_t cnt = vsnprintf(log_buf, sizeof(log_buf), format, args);
    printf("%s", log_buf);
    printf("\n");
    return cnt;
}

/* TODO:adapt to your platform ----------------------------------------------*/

template_status_t htemplate1_malloc(uint16_t size, void **pp)
{
    *pp = malloc(size);
    if (*pp == NULL)
    {
        return TEMPLATE_STATUS_RESOURCE_ERROR;
    }
    g_malloc_cnt++;
    ex_template_log(0, "[%p]malloc %d bytes, cnt:%d", *pp, size, g_malloc_cnt);
    return TEMPLATE_STATUS_OK;
}
void htemplate1_free(void *p)
{
    free(p);
    g_free_cnt++;
    ex_template_log(0, "[%p]free cnt:%d\n", p, g_free_cnt);
}

template_status_t htemplate1_write(uint8_t *data, uint16_t len)
{
    for (size_t i = 0; i < len; i++)
    {
        fputc(data[i], stdout);
    }
    return TEMPLATE_STATUS_OK;
}
template_status_t htemplate1_read(uint8_t *data, uint16_t len)
{
    ex_template_log(0, "read %d bytes:", len);
    for (size_t i = 0; i < len; i++)
    {
        data[i] = getchar();
    }
    return TEMPLATE_STATUS_OK;
}
template_status_t htemplate1_ctrl(uint32_t cmd, void *param)
{
    ex_template_log(0, (const char *)param);
    return TEMPLATE_STATUS_OK;
}

template_ex_port_t htemplate1_ex_port =
{
    .pf_malloc = htemplate1_malloc,
    .pf_free = htemplate1_free,
    .pf_write = htemplate1_write,
    .pf_read = htemplate1_read,
    .pf_ctrl = htemplate1_ctrl,
};
/* adapt end ----------------------------------------------------------------*/

/**
 * @brief 
 * @retval int 
 */
int main(int argc , char const *argv[])
{
    uint8_t buf[10] = {0};
    template_constructor(&htemplate1, &htemplate1_ex_port);
    template_read(&htemplate1, &buf[0], 9);
    template_write(&htemplate1, &buf[0], 9);
    template_destructor(&htemplate1);
    ex_template_log(0, "g_malloc_cnt:%d, g_free_cnt:%d\n", g_malloc_cnt, g_free_cnt);
    return 0;
}

/*-----------------------------------------------------------------------------
|                           Funciton definition end                           |
-----------------------------------------------------------------------------*/
